#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import json
import socket
import struct

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect_ex(("127.0.0.1", 2007))

while True:
    inp = input(">>>: ").strip()
    if not inp: continue
    client.send(inp.encode("utf-8"))
    # 1、获取报头,先接收第一个4个字节
    data = client.recv(4)
    total_size = struct.unpack("i", data)[0]
    # 2、 获取到字典的固定长度之后, 直接获取字典的大小
    header_dic = client.recv(total_size)
    # 接收到的是bytes格式，转换成utf-8格式, 然后在使用json
    header_json = json.loads(header_dic)

    recv_size = 0
    res = b""
    while recv_size < header_json["total_size"]:
        data = client.recv(1024)
        res += data
        recv_size += len(data)
    print(res.decode("gbk"))
